package com.zb.frame.modules.sys.upgrade;

import com.zb.frame.base.upgrade.common.UpgradeTask;
import com.zb.frame.base.upgrade.enums.DBType;
import com.zb.frame.base.utils.encrypt.SimpleHashUtil;
import com.zb.frame.modules.sys.utils.UpgradeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;


public class UpgradeTask_0_0_1 extends UpgradeTask {
	
	private static final Logger logger = LoggerFactory.getLogger(UpgradeTask_0_0_1.class);

	@Override
	public boolean doUpgrade(Connection conn, DBType type) {
		try {

			// 初始化 用户
			UpgradeUtils.insertDataUser(conn,"sys_user","admin", SimpleHashUtil.encode("111111"),"超级管理员","13660270951","448341949@qq.com","","");
			UpgradeUtils.insertDataUser(conn,"sys_user","manager", SimpleHashUtil.encode("111111"),"管理员","","","","");
			UpgradeUtils.insertDataUser(conn,"sys_user","bangZZ", SimpleHashUtil.encode("111111"),"周大侠","","","","");

			// 初始化 角色
			UpgradeUtils.insertDataRole(conn,"sys_role","admin","超级管理员","");
			UpgradeUtils.insertDataRole(conn,"sys_role","manager","管理员","");
			UpgradeUtils.insertDataRole(conn,"sys_role","user","普通用户","");

			// 初始化 用户角色关系
			UpgradeUtils.insertDataUserRoleRel(conn, "sys_rel_user_role", 1L, "ADMIN");
			UpgradeUtils.insertDataUserRoleRel(conn, "sys_rel_user_role", 2L, "MANAGER");
			UpgradeUtils.insertDataUserRoleRel(conn, "sys_rel_user_role", 3L, "USER");

			// 初始化 菜单资源
			UpgradeUtils.insertDataMenu(conn, "sys_menu",0L,"sys","系统管理","el-icon-s-tools",1,"","","系统管理目录", 1);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",0L,"monitor","日志监控","el-icon-s-platform",1,"","","日志监控目录", 2);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",1L,"user","用户管理","el-icon-user-solid",2,"sys/user","","系统管理目录>用户管理", 1);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",1L,"role","角色管理","el-icon-s-custom",2,"sys/role","","系统管理目录>角色管理", 2);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",1L,"menu","菜单管理","el-icon-menu",2,"sys/menu","","系统管理目录>菜单管理", 3);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",2L,"log","系统日志","el-icon-date",2,"monitor/log","","日志监控目录>日志管理", 1);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",3L,"user_view","查看","",3,"","sys:user:view","系统管理目录>用户管理>查看操作", 1);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",3L,"user_add","添加","",3,"","sys:user:add","系统管理目录>用户管理>新增操作", 2);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",3L,"user_edit","编辑","",3,"","sys:user:edit","系统管理目录>用户管理>编辑操作", 3);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",3L,"user_switch","停/启用","",3,"","sys:user:switch","系统管理目录>用户管理>停/启用操作", 4);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",3L,"user_auth","授权","",3,"","sys:user:auth","系统管理目录>用户管理>授权角色操作", 5);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",4L,"role_view","查看","",3,"","sys:role:view","系统管理目录>角色管理>查看操作", 1);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",4L,"role_add","添加","",3,"","sys:role:add","系统管理目录>角色管理>新增操作", 2);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",4L,"role_edit","编辑","",3,"","sys:role:edit","系统管理目录>角色管理>编辑操作", 3);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",4L,"role_del","删除","",3,"","sys:role:del","系统管理目录>角色管理>删除操作", 4);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",4L,"role_auth","授权","",3,"","sys:role:auth","系统管理目录>角色管理>授权角色操作", 5);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",5L,"menu_view","查看","",3,"","sys:menu:view","系统管理目录>菜单管理>查看操作", 1);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",5L,"menu_add","添加","",3,"","sys:menu:add","系统管理目录>菜单管理>新增操作", 2);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",5L,"menu_edit","编辑","",3,"","sys:menu:edit","系统管理目录>菜单管理>编辑操作", 3);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",5L,"menu_del","删除","",3,"","sys:menu:del","系统管理目录>菜单管理>删除操作", 4);
			UpgradeUtils.insertDataMenu(conn, "sys_menu",6L,"log_view","查看","",3,"","monitor:log:view","日志监控目录>日志管理>查看操作", 1);

			// 初始化 角色菜单资源关系
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 1L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 2L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 3L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 4L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 5L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 6L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 7L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 12L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 17L);
			UpgradeUtils.insertDataMenuRoleRel(conn, "sys_rel_role_menu", "MANAGER", 21L);

 			return true;
		} catch(Exception e) {
			logger.error("Upgrade module '" + this.getClass().getPackage().getName() + "' to " + getNewVersion() + " fail.", e);
			return false;
		}
	}

	@Override
	public String getNewVersion() {
		return "0.0.2";
	}

}
